package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.DriverRebate;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverRebateService;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine 再用
 */
@Controller
@RequestMapping(value = "driverrebate")
public class DriverRebateController {

    @Autowired
    private DriverRebateService driverRebateService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "rebate", method = RequestMethod.GET)
    public ModelAndView list() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("driverrebate")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("driverrebate/rebate");

            DriverRebate driverRebate = driverRebateService
                .findByDriverId(principal.getCompanyId());
            if (null != driverRebate) {
                mv.addObject("rebate", driverRebate);
            } else {
                mv.addObject("rebate", new DriverRebate());
            }

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Result<DriverRebate> update(DriverRebate driverRebate, Model model) {

        Result<DriverRebate> vo = new Result<DriverRebate>();

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("driverRebate")) {

            if (null == driverRebate.getDriverId()) {
                vo.setSuccess(false);
                vo.setMessage("您没有选中司机");
                return vo;
            }
            driverRebateService.save(driverRebate);
            DriverRebate local = driverRebateService.findOne(driverRebate.getDriverId());

            local.setType(driverRebate.getType());
            local.setLevel(driverRebate.getLevel());
            if (driverRebate.getType().equals(0)) {
                local.setDisposableMoney1(driverRebate.getDisposableMoney1());
            } else if (driverRebate.getType().equals(1)) {
                local.setCommission1(driverRebate.getCommission1());

                local.setMoney1(driverRebate.getMoney1());

                local.setDriverRebateType1(driverRebate.getDriverRebateType1());
            }

            driverRebateService.update(local);

            String detail = "【%s】修改了服务人员推荐提成";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            vo.setSuccess(true);
        } else {
            vo.setSuccess(false);
            vo.setMessage("您没有该权限");
        }

        return vo;

    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }

}

